<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/favicon.ico">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon.ico">
  <link rel="mask-icon" href="/images/favicon.ico" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/","scheme":"Mist","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"always","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="加密算法笔记">
<meta property="og:type" content="article">
<meta property="og:title" content="加密算法">
<meta property="og:url" content="http://yoursite.com/2018/08/14/tech-2018-08-14-Encrypt/index.html">
<meta property="og:site_name" content="Akiyama&#39;s Blog">
<meta property="og:description" content="加密算法笔记">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2018-08-13T16:00:00.000Z">
<meta property="article:modified_time" content="2019-02-08T16:57:46.888Z">
<meta property="article:author" content="丘山月夜">
<meta property="article:tag" content="技术">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://yoursite.com/2018/08/14/tech-2018-08-14-Encrypt/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>加密算法 | Akiyama's Blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Akiyama's Blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">丘山月夜的博客</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="home fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="user fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="tags fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="th fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="archive fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/08/14/tech-2018-08-14-Encrypt/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.png">
      <meta itemprop="name" content="丘山月夜">
      <meta itemprop="description" content="君は空を見てるか,<br>風の音を聞いてるか">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Akiyama's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          加密算法
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2018-08-14 00:00:00" itemprop="dateCreated datePublished" datetime="2018-08-14T00:00:00+08:00">2018-08-14</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2019-02-09 00:57:46" itemprop="dateModified" datetime="2019-02-09T00:57:46+08:00">2019-02-09</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/" itemprop="url" rel="index"><span itemprop="name"></span></a>
                </span>
            </span>

          
            <div class="post-description">加密算法笔记</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="密钥交换算法"><a href="#密钥交换算法" class="headerlink" title="密钥交换算法"></a>密钥交换算法</h2><h3 id="Diffie-Hellman密钥交换算法原理："><a href="#Diffie-Hellman密钥交换算法原理：" class="headerlink" title="Diffie-Hellman密钥交换算法原理："></a>Diffie-Hellman密钥交换算法原理：</h3><ol>
<li>Alice与Bob确定两个大素数n和g，这两个数不用保密</li>
<li>Alice选择另一个大随机数x，并计算A如下：A=gx mod n</li>
<li>Alice将A发给Bob</li>
<li>Bob  选择另一个大随机数y，并计算B如下：B=gy mod n</li>
<li>Bob将B发给Alice</li>
<li>计算秘密密钥K1如下：K1=Bx mod n</li>
<li>计算秘密密钥K2如下：K2=Ay mod n</li>
<li>K1=K2，因此Alice和Bob可以用其进行加解密</li>
</ol>
<h3 id="RSA加密算法"><a href="#RSA加密算法" class="headerlink" title="RSA加密算法"></a>RSA加密算法</h3><p>rsa加密算法是基于这样的数学事实：两个大素数（质数）相乘容易，而对得到的乘积求因子则很难。加密过程如下：</p>
<ol>
<li>选择两个大素数P、Q</li>
<li>计算N=P*Q</li>
<li>选择一个公钥（加密密钥）E，使其不是(P-1)与(Q-1)的因子</li>
<li>选择私钥（解密密钥）D，满足条件：<code>(D*E) mod (P-1)(Q-1)=1</code></li>
<li>加密时，明文PT计算密文CT：<code>CT=PTE mod N</code></li>
<li>解密时，从密文CT计算明文PT：PT=CTDmodN 这也是SSL中会用一种密钥交换算法。</li>
</ol>
<h2 id="公钥和私钥、加密和解密、签名和认证"><a href="#公钥和私钥、加密和解密、签名和认证" class="headerlink" title="公钥和私钥、加密和解密、签名和认证"></a>公钥和私钥、加密和解密、签名和认证</h2><p>公钥（public key）可以公开，私钥（private key）自己持有。用其中一个进行加密，只能用另一个进行解密</p>
<h3 id="在加密的场景下，公钥用来加密，私钥用来解密。"><a href="#在加密的场景下，公钥用来加密，私钥用来解密。" class="headerlink" title="在加密的场景下，公钥用来加密，私钥用来解密。"></a>在加密的场景下，<strong>公钥用来加密，私钥用来解密</strong>。</h3><p>Bob用Alice的公钥对消息进行加密，并把加密之后的消息传回给Alice；Alice应该保存好与这个公钥相对应的私钥，当Alice收到Bob发来的加密过的消息后，通过手里的私钥对其进行解密。</p>
<p>只有私钥可以解密，而公钥不可以，所以即使公钥被窃听者Eve知道也没有关系。</p>
<h3 id="在签名的场景下，私钥用来加密，公钥用来解密"><a href="#在签名的场景下，私钥用来加密，公钥用来解密" class="headerlink" title="在签名的场景下，私钥用来加密，公钥用来解密"></a>在签名的场景下，私钥用来加密，公钥用来解密</h3><p>签名是对上述过程的反向应用，即在此场景下，用私钥进行“加密”，而公钥进行“解密”，更清晰的说法是：在此场景下，用私钥进行“签名”，用公钥进行“认证”。目的是保证内容是Alice发出的</p>
<p>因为私钥只有Alice自己知道，所以用私钥对消息进行签名后，只有与之对应的公钥才能对这个签过名的消息进行认证，而公钥是公开的，通过这个过程可以知道该消息确实是属于Alice的。</p>
<p>如使用ssh登录GitHub仓库，本地持有私钥，服务器发送一段文本，本地使用私钥进行加密发送给服务器，服务器进行解密并验证通过</p>
<p><strong>参考</strong></p>
<p><a target="_blank" rel="noopener" href="https://www.zhihu.com/question/25912483">知乎：RSA的公钥和私钥到底哪个才是用来加密和哪个用来解密？</a></p>

    </div>

    
    
    
        

  <div class="followme">
    <p>欢迎关注我的其它发布渠道</p>

    <div class="social-list">

        <div class="social-item">
          <a target="_blank" class="social-link" href="/images/wechat_channel.jpg">
            <span class="icon">
              <i class="fab fa-weixin"></i>
            </span>

            <span class="label">WeChat</span>
          </a>
        </div>
    </div>
  </div>


      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/" rel="tag"><i class="fa fa-tag"></i> </a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2018/08/04/android-2018-08-04-DependenciesNote/" rel="prev" title="Android依赖配置">
      <i class="fa fa-chevron-left"></i> Android依赖配置
    </a></div>
      <div class="post-nav-item">
    <a href="/2018/08/17/basic-2018-08-17-gcc-g/" rel="next" title="gcc和g++编译器">
      gcc和g++编译器 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="header-overlay"></div>
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AF%86%E9%92%A5%E4%BA%A4%E6%8D%A2%E7%AE%97%E6%B3%95"><span class="nav-number">1.</span> <span class="nav-text">密钥交换算法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Diffie-Hellman%E5%AF%86%E9%92%A5%E4%BA%A4%E6%8D%A2%E7%AE%97%E6%B3%95%E5%8E%9F%E7%90%86%EF%BC%9A"><span class="nav-number">1.1.</span> <span class="nav-text">Diffie-Hellman密钥交换算法原理：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#RSA%E5%8A%A0%E5%AF%86%E7%AE%97%E6%B3%95"><span class="nav-number">1.2.</span> <span class="nav-text">RSA加密算法</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%AC%E9%92%A5%E5%92%8C%E7%A7%81%E9%92%A5%E3%80%81%E5%8A%A0%E5%AF%86%E5%92%8C%E8%A7%A3%E5%AF%86%E3%80%81%E7%AD%BE%E5%90%8D%E5%92%8C%E8%AE%A4%E8%AF%81"><span class="nav-number">2.</span> <span class="nav-text">公钥和私钥、加密和解密、签名和认证</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9C%A8%E5%8A%A0%E5%AF%86%E7%9A%84%E5%9C%BA%E6%99%AF%E4%B8%8B%EF%BC%8C%E5%85%AC%E9%92%A5%E7%94%A8%E6%9D%A5%E5%8A%A0%E5%AF%86%EF%BC%8C%E7%A7%81%E9%92%A5%E7%94%A8%E6%9D%A5%E8%A7%A3%E5%AF%86%E3%80%82"><span class="nav-number">2.1.</span> <span class="nav-text">在加密的场景下，公钥用来加密，私钥用来解密。</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9C%A8%E7%AD%BE%E5%90%8D%E7%9A%84%E5%9C%BA%E6%99%AF%E4%B8%8B%EF%BC%8C%E7%A7%81%E9%92%A5%E7%94%A8%E6%9D%A5%E5%8A%A0%E5%AF%86%EF%BC%8C%E5%85%AC%E9%92%A5%E7%94%A8%E6%9D%A5%E8%A7%A3%E5%AF%86"><span class="nav-number">2.2.</span> <span class="nav-text">在签名的场景下，私钥用来加密，公钥用来解密</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="丘山月夜"
      src="/images/avatar.png">
  <p class="site-author-name" itemprop="name">丘山月夜</p>
  <div class="site-description" itemprop="description">君は空を見てるか,<br>風の音を聞いてるか</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">60</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">21</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">丘山月夜</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://mist.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
